<?php

declare(strict_types=1);

namespace App\Services;

use Hyperf\Di\Annotation\Inject;
use App\Exception\ApiException;
use App\Constants\ErrorCode;
use App\Model\News;

class NewsService extends BaseService
{

    public function list(){
        $model = News::query()->select('id','title','image','category','content','author','created_at');
        $list = $this->usePage($model)->toArray();

        return $list;
    }

    public function detail(array $params)
    {
        $news = News::query()->select('id','title','image','category','content','author','created_at')->where('id', $params['id'])->first();
        if(!$news)
        {
            throw new ApiException(ErrorCode::WARNING, '内容不存在');
        }

        foreach($news as $key=>$val)
        {
            $news[$key]['date'] = date('Y-m-d', strtotime($val['created_at']));
        }

        return $news;
    }

}